package cn.fay.chapter16;

import java.util.Arrays;
import java.util.Random;
/**
 * 非常巧妙的方法，利用do while循环和boolean数组的特性，
 * 从一个已有的字符串数组里抽出随机的不重复的
 * 内容构建新的字符串数组。
 * @author emellend
 *
 */
public class IceCream {
	private static Random random=new Random();
	static final String[] FLAVORS={
		"Chocolate","Strawberry","Vanilla Fudge Swirl",
		"Mini Chip","Mocha Almond Fudge","Rum Raisin",
		"Praline Cream","Mud Pie"
	};
	public static String[] flavorSet(int n) {
		if (n>FLAVORS.length) {
			throw new IllegalArgumentException("Set too big");
		}
		String[] result=new String[n];
		boolean[] picked=new boolean[FLAVORS.length];
		for (int i = 0; i < n; i++) {
			int t;
			do {
				t=random.nextInt(FLAVORS.length);
			} while (picked[t]);
			result[i]=FLAVORS[t];
			picked[t]=true;
		}
		return result;
	}
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(Arrays.toString(flavorSet(5)));
		}
	}
}
